namespace Roids.SL
{
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    public class TitleOverlay : DrawableGameComponent
    {
        private const double TimeToFade = 5;
        private readonly RoidsGame _game;
        private double _opacity = 1f;
        private Vector2 _position;
        private Texture2D _title;

        public TitleOverlay(RoidsGame game)
            : base(game)
        {
            _game = game;
        }

        protected override void LoadContent()
        {
            _title = _game.Content.Load<Texture2D>(@"Sprites\Title");
            base.LoadContent();
        }

        public override void Initialize()
        {
            var bounds = _game.Window.ClientBounds;
            _position = new Vector2(
                bounds.Width/2 - 200,
                bounds.Height/3 - 70);

            base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            if (_opacity < 0) return;
            _opacity = (TimeToFade - gameTime.TotalGameTime.TotalSeconds)/TimeToFade;
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (_opacity < 0) return;
            var batch = new SpriteBatch(_game.GraphicsDevice);
            var fade = new Color(255, 255, 255, (byte) MathHelper.Clamp((float) _opacity*255, 0, 255));

            batch.Begin(SpriteBlendMode.AlphaBlend);
            batch.Draw(_title, _position, fade);
            batch.End();
            base.Draw(gameTime);
        }
    }
}